package org.fxml.utils {

	import flash.display.DisplayObject;

	/**
	 * @author jordandoczy
	 * @private
	 */
	public class Converter {
		
		public static function toNumber(value:String):Number{
			
			if(value.indexOf("#") != -1) return Number(value.replace("#", "0x"));
			
			var n:Number;
			try{
				n = Number(value);
			}
			catch(e:Error){}
			
			return n;
		}
		
		public static function convertValue(value:String, root:DisplayObject=null):*{
				 if(isBoolean(value)) 	return BooleanUtil(value);
			else if(isNumeric(value)) 	return toNumber(value);
			else if(root)				return convertFlashVar(value, root);
			else						return value;
		}
		
		public static function convertFlashVar(value:String, root:DisplayObject):String{
			var reg:RegExp = /flashvars\.([A-Za-z0-9_]+)/i; 
			
			var results:Array = reg.exec(value);
			var result:String = value;
			
			if(results && results.length > 1){
				var variable:String = results[1];
				result = FlashVarUtil.getValue(root, variable);
				value = value.replace(results[0], result);
			}
			
			return value;
		}
		
		public static function isNumeric(value:String):Boolean{
			var reg:RegExp = /[^\d.]/g; // look for any non-digit characters (also ignore the ".") 
			return !reg.test(value);
		}
		
		public static function isBoolean(value:String):Boolean{
			return value.toLowerCase() === "true" || value.toLowerCase() === "false";
		}
	}
}
